package cn.hfatec.freemarker;

import cn.hfatec.app.AppContext;
import cn.hfatec.web.WebContext;
import freemarker.ext.servlet.FreemarkerServlet;
import freemarker.ext.servlet.HttpRequestHashModel;
import freemarker.ext.servlet.IncludePage;
import freemarker.ext.servlet.ServletContextHashModel;
import freemarker.template.ObjectWrapper;
import freemarker.template.Template;
import org.springframework.ui.Model;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: panwb
 * <p/>
 * Date: 2015/3/3
 * Time: 17:21
 */
public class FreemarkerRender extends RenderUtil {

    public Map render(String name, Model model) throws Exception {
        FreeMarkerConfigurer configurer = AppContext.getBean(FreeMarkerConfigurer.class);
        setModelInfo(model);
        Template template = configurer.getConfiguration().getTemplate(name + ".ftl");
        StringWriter writer = new StringWriter();
        template.process(model, writer);
        String result = writer.toString();
        Map node = new HashMap();
        node.put("data", result);
        return node;
    }

    @Override
    public String render(String name, Map map) throws Exception {

        FreeMarkerConfigurer configurer = AppContext.getBean(FreeMarkerConfigurer.class);
        Template template = configurer.getConfiguration().getTemplate(name + ".ftl");
        StringWriter writer = new StringWriter();
        template.process(map, writer);
        return writer.toString();
    }

    private void setModelInfo(Model model) {
        if (WebContext.getRequest() != null) {
            ServletContext servletContext = WebContext.getRequest().getSession().getServletContext();
            model.addAttribute(FreemarkerServlet.KEY_REQUEST, WebContext.getRequest());
            model.addAttribute(FreemarkerServlet.KEY_INCLUDE, new IncludePage(WebContext.getRequest(), WebContext.getResponse()));
            model.addAttribute(FreemarkerServlet.KEY_REQUEST_PRIVATE, (HttpRequestHashModel) WebContext.getRequest().getAttribute(".freemarker.Request"));
            model.addAttribute(FreemarkerServlet.KEY_REQUEST_PARAMETERS, servletContext.getAttribute(FreemarkerServlet.KEY_REQUEST_PARAMETERS));
            model.addAttribute(FreemarkerServlet.KEY_SESSION, WebContext.getRequest().getSession());
            model.addAttribute(FreemarkerServlet.KEY_APPLICATION, servletContext.getAttribute(FreemarkerServlet.KEY_APPLICATION));
            model.addAttribute(FreemarkerServlet.KEY_APPLICATION_PRIVATE, servletContext.getAttribute(FreemarkerServlet.KEY_APPLICATION_PRIVATE));
            FreeMarkerConfigurer bean = AppContext.getBean(FreeMarkerConfigurer.class);
            if (bean.getTaglibFactory() == null) {
                System.out.println("tag lib factorty null");
            }
            model.addAttribute(FreemarkerServlet.KEY_JSP_TAGLIBS, bean.getTaglibFactory());
            HttpServletRequest request = WebContext.getRequest();
            ServletContextHashModel servletContextModel = (ServletContextHashModel) servletContext.getAttribute(".freemarker.Application");
            model.addAttribute(".freemarker.Application", servletContextModel);
            model.addAttribute("__FreeMarkerServlet.Application__", servletContextModel);
            HttpRequestHashModel requestModel = new HttpRequestHashModel(request, WebContext.getResponse(), ObjectWrapper.DEFAULT_WRAPPER);
            model.addAttribute("__FreeMarkerServlet.Request__", requestModel);
        }
    }
}
